#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: ningbo_gs_event.py
@time: 2018/2/5 17:03
"""
import re
import sys
import time

import click

sys.path.append('../..')
from common import util

from common.mongo import MongDb
from logger import Logger

APP_DATA_CONF = {
    'host': '172.16.215.16',
    'port': 40042,
    'db': 'app_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('ningbo_gs_event.log').get_logger()


class GsEvent(object):
    # 原始数据表
    SOURCE_TABLE = 'enterprise_data_gov_new_2018_01_08'

    # 定义分类表
    # 分支机构新增表
    ZHEJIANG_NINGBO_GS_BRANCH_TABLE = 'zhejiang_ningbo_gs_branch'
    # 注册资本新增表
    ZHEJIANG_NINGBO_GS_REGISTERED_CAPITAL_TABLE = 'zhejiang_ningbo_gs_registered_capital'
    # 负责人变更表
    ZHEJIANG_NINGBO_GS_LEGAL_MAN_TABLE = 'zhejiang_ningbo_gs_legal_man'
    # 企业名称变更表
    ZHEJIANG_NINGBO_GS_COMPANY_NAME_TABLE = 'zhejiang_ningbo_gs_company_name'
    # 经营范围变更表
    ZHEJIANG_NINGBO_GS_BUSINESS_SCOPE_TABLE = 'zhejiang_ningbo_gs_business_scope'

    def __init__(self, delay_days, _log):
        self.log = _log
        # 获得今天的时间
        self.current_day = util.get_now_date()

        # 获得分界时间
        self.last_day = util.get_pre_day(self.current_day, delay_days)

        self.app_data_db = MongDb(APP_DATA_CONF['host'], APP_DATA_CONF['port'], APP_DATA_CONF['db'],
                                  APP_DATA_CONF['username'],
                                  APP_DATA_CONF['password'], log=self.log)
        self.title_set = set()

        # 结果存储
        self.result_list_dict = {
            self.ZHEJIANG_NINGBO_GS_BRANCH_TABLE: [],
            self.ZHEJIANG_NINGBO_GS_REGISTERED_CAPITAL_TABLE: [],
            self.ZHEJIANG_NINGBO_GS_LEGAL_MAN_TABLE: [],
            self.ZHEJIANG_NINGBO_GS_COMPANY_NAME_TABLE: [],
            self.ZHEJIANG_NINGBO_GS_BUSINESS_SCOPE_TABLE: []
        }

        # 关键字
        self.keyword_list_dict = {
            self.ZHEJIANG_NINGBO_GS_BRANCH_TABLE: ['分支'],
            self.ZHEJIANG_NINGBO_GS_REGISTERED_CAPITAL_TABLE: ['注册资本'],
            self.ZHEJIANG_NINGBO_GS_LEGAL_MAN_TABLE: ['负责人变更', '法定代表人变更'],
            self.ZHEJIANG_NINGBO_GS_COMPANY_NAME_TABLE: ['名称变更'],
            self.ZHEJIANG_NINGBO_GS_BUSINESS_SCOPE_TABLE: ['经营范围']
        }

        pattern = '\d+\.?\d*'
        self.regex = re.compile(pattern)

        log.info("当前时间: {} 历史分界时间: {}".format(self.current_day, self.last_day))

        # 先删除当前的表
        self.delete_tables()

    def delete_tables(self):
        self.app_data_db.drop(self.ZHEJIANG_NINGBO_GS_BRANCH_TABLE)
        self.app_data_db.drop(self.ZHEJIANG_NINGBO_GS_REGISTERED_CAPITAL_TABLE)
        self.app_data_db.drop(self.ZHEJIANG_NINGBO_GS_LEGAL_MAN_TABLE)
        self.app_data_db.drop(self.ZHEJIANG_NINGBO_GS_COMPANY_NAME_TABLE)
        self.app_data_db.drop(self.ZHEJIANG_NINGBO_GS_BUSINESS_SCOPE_TABLE)
        self.log.info("删除表完成。。。")
        time.sleep(3)

    def start_parse(self):
        count = 0
        for item in self.app_data_db.traverse_batch(self.SOURCE_TABLE, {'province': '浙江'}):
            # 判断是否符合条件
            self.select_match_item(item)

            count += 1
            if count % 1000 == 0:
                log.info("当前进度: {}".format(count))

        log.info("完成遍历: {}".format(count))

        for key in self.result_list_dict.keys():
            self.store_result(key)
            self.log.info("存储结果完成: table = {}".format(key))

        log.info("结果存储完成...")

    # 输出变更事项类型
    def print_title_set(self):
        self.log.info("变更事项数目: len = {}".format(len(self.title_set)))
        for title in self.title_set:
            log.info(title)

    # 判断分支机构
    def is_branch(self, before, after):

        if not isinstance(after, basestring):
            return False

        # 最开始没有到有
        if not isinstance(before, basestring):
            return True

        before_list = before.split(' ')
        after_list = after.split(' ')

        if len(after_list) > len(before_list):
            return True

        return False

    # 正则提取数据
    def __get_value(self, value_str):

        search_list = self.regex.findall(value_str)
        try:
            if len(search_list) > 0:
                return float(search_list[0])
        except:
            return 0

        return 0

    # 注册资本增加
    # todo 正则提取数值
    def is_registered_capital(self, before, after):

        if not isinstance(before, basestring):
            return True

        if not isinstance(after, basestring):
            return False

        after_value = self.__get_value(after)
        before_value = self.__get_value(before)

        if after_value > before_value:
            return True

        return False

    # 判断名称
    def is_default(self, before, after):
        return True

    # 存储结果
    def store_result(self, key):
        self.app_data_db.insert_batch_data(key, self.result_list_dict[key])
        del self.result_list_dict[key][:]

    def select_match_item(self, gs_item):

        # 提取变更信息
        change_record_list = gs_item.get('changerecords')
        if not isinstance(change_record_list, list):
            return

        if len(change_record_list) <= 0:
            return

        # 判断这些属性是否已经添加了
        success_dict = {
            self.ZHEJIANG_NINGBO_GS_BRANCH_TABLE: False,
            self.ZHEJIANG_NINGBO_GS_REGISTERED_CAPITAL_TABLE: False,
            self.ZHEJIANG_NINGBO_GS_LEGAL_MAN_TABLE: False,
            self.ZHEJIANG_NINGBO_GS_COMPANY_NAME_TABLE: False,
            self.ZHEJIANG_NINGBO_GS_BUSINESS_SCOPE_TABLE: False
        }

        func_dict = {
            self.ZHEJIANG_NINGBO_GS_BRANCH_TABLE: self.is_branch,
            self.ZHEJIANG_NINGBO_GS_REGISTERED_CAPITAL_TABLE: self.is_registered_capital,
            self.ZHEJIANG_NINGBO_GS_LEGAL_MAN_TABLE: self.is_default,
            self.ZHEJIANG_NINGBO_GS_COMPANY_NAME_TABLE: self.is_default,
            self.ZHEJIANG_NINGBO_GS_BUSINESS_SCOPE_TABLE: self.is_default,
        }

        # # 判断这些属性是否已经添加了
        # is_gs_branch = False
        # is_gs_registered_capital = False
        # is_gs_legal_man = False
        # is_gs_company_name = False
        # is_gs_business_scope = False
        found_count = 0

        change_record_sort_list = sorted(change_record_list, key=lambda it: it['change_date'], reverse=True)

        for item in change_record_sort_list:

            if found_count >= 5:
                return

            change_date = item.get('change_date')
            if change_date < self.last_day:
                continue

            change_title = item.get('change_item')
            if not isinstance(change_title, basestring):
                continue

            change_title = change_title.strip()
            self.title_set.add(change_title)

            before_content = item.get('before_content')
            after_content = item.get('after_content')
            for key, is_success in success_dict.iteritems():
                if is_success:
                    continue

                # 挨个遍历关键字是否在变更事项中
                for keyword in self.keyword_list_dict[key]:
                    if keyword in change_title:
                        func = func_dict[key]

                        status = func(before_content, after_content)
                        if status is True:
                            found_count += 1
                            success_dict[key] = True
                            item_list = self.result_list_dict[key]
                            item_list.append(gs_item)

                            # 如果数目达到上限则先存储
                            if len(item_list) >= 200:
                                # 判断是否需要存储结果
                                self.store_result(key)
                        break


@click.command()
@click.option('--delay',
              default=30,
              type=int,
              help='延迟天数')
def main(delay):
    gs_event = GsEvent(delay_days=delay, _log=log)
    gs_event.start_parse()
    gs_event.print_title_set()


if __name__ == '__main__':
    main()
